﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Data.SqlClient;

/// <summary>
/// 返回結果
/// </summary>
[DataContract]
class RouteMarker_Stop
{
    /// <summary>
    /// 站牌ID
    /// </summary>
    [DataMember(Order = 0)]
    public int id { get; set; }
    /// <summary>
    /// 城市
    /// </summary>
    [DataMember(Order = 1)]
    public string city { get; set; }
    /// <summary>
    /// 去回程
    /// </summary>
    [DataMember(Order = 2)]
    public string goback { get; set; }
    /// <summary>
    /// 路線ID
    /// </summary>
    [DataMember(Order = 3)]
    public string route_id { get; set; }
    /// <summary>
    /// 站牌名稱
    /// </summary>
    [DataMember(Order = 4)]
    public string name { get; set; }
    /// <summary>
    /// 經度
    /// </summary>
    [DataMember(Order = 5)]
    public float lat { get; set; }
    /// <summary>
    /// 緯度
    /// </summary>
    [DataMember(Order = 6)]
    public float lng { get; set; }
    /// <summary>
    /// 地标
    /// </summary>
    [DataMember(Order = 7)]
    public IList<RouteMarker_Marker> marker_class { get; set; }
}
[DataContract]
class RouteMarker_Marker
{
    [DataMember(Order = 0)]
    public string id { get; set; }
    [DataMember(Order = 1)]
    public string name { get; set; }
}
[DataContract]
class RouteMarker_Route
{
    /// <summary>
    /// 线路ID
    /// </summary>
    [DataMember(Order = 0)]
    public string id { get; set; }
    /// <summary>
    /// 线路名
    /// </summary>
    [DataMember(Order = 1)]
    public string name { get; set; }

    /// <summary>
    /// 起點
    /// </summary>
    [DataMember(Order = 2)]
    public string from { get; set; }

    /// <summary>
    /// 目的地
    /// </summary>
    [DataMember(Order = 3)]
    public string to { get; set; }
}

[DataContract]
class RouteMarker_Result
{
    /// <summary>
    /// 线路名
    /// </summary>
    [DataMember(Order = 0)]
    public RouteMarker_Route route { get; set; }
    /// <summary>
    /// 站牌
    /// </summary>
    [DataMember(Order = 1)]
    public IList<RouteMarker_Stop> stop { get; set; }

}

public partial class Service_RouteMarker : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["rtid"]) && !string.IsNullOrEmpty(Request.QueryString["city"]))
        {
            using (MyDb.Db db = new MyDb.Db())
            {
                RouteMarker_Result result = new RouteMarker_Result();
                result.stop = new List<RouteMarker_Stop>();
                string gobsql = "";
                if (!string.IsNullOrEmpty(Request.QueryString["gob"])) {
                    gobsql = string.Format("and c.goBack ='{0}'",Request.QueryString["gob"]);
                }
                using (DataTable DT = db.getDataTable(string.Format("select a.pathAttributeId,a.nameZh as routename,a.departureZh,a.destinationZh,c.Id as stopid,c.city,c.goBack,c.nameZh as stopname,c.latitude,c.longitude,d.MarkerClassID,e.NameZh as classname,b.sequenceNo from route a left join PathDetail b on a.pathAttributeId=b.pathAttributeId left join Stop c on b.stopId=c.Id and c.city='tpi' full join stopmarkerclass_cache d on d.stopid=c.ID and d.City=c.city left join MarkerClass e on d.MarkerClassID=e.ID  where a.pathAttributeId='{0}' and c.city='{1}' {2} group by a.pathAttributeId,a.nameZh,a.departureZh,a.destinationZh,c.Id,c.city,c.goBack,c.nameZh,c.latitude,c.longitude,d.MarkerClassID,e.NameZh,b.sequenceNo,e.Orders order by b.sequenceNo,e.Orders ASC ", Request.QueryString["rtid"], Request.QueryString["city"], gobsql), "mess"))
                {
                    RouteMarker_Route route = new RouteMarker_Route();
                    route.id = DT.Rows[0]["pathAttributeId"].ToString();
                    route.name = DT.Rows[0]["routename"].ToString();
                    if (!string.IsNullOrEmpty(Request.QueryString["gob"])) {
                        if (Request.QueryString["gob"].ToString() == "0")
                        {
                            route.from = DT.Rows[0]["departureZh"].ToString();
                            route.to = DT.Rows[0]["destinationZh"].ToString();
                        }
                        else
                        {
                            route.from = DT.Rows[0]["destinationZh"].ToString();
                            route.to = DT.Rows[0]["departureZh"].ToString();
                        }
                    }
                    result.route = route;
                    result.stop = new List<RouteMarker_Stop>();

                    string stopid = "";

                    foreach (DataRow row in DT.Rows)
                    {
                        if (stopid != row["stopid"].ToString()) {
                            RouteMarker_Stop item = new RouteMarker_Stop();
                            item.id = Convert.ToInt32(row["stopid"]);
                            item.city = row["city"].ToString();
                            item.goback = row["goBack"].ToString();
                            item.route_id = row["pathAttributeId"].ToString();
                            item.name = row["stopname"].ToString();
                            item.lat = float.Parse(row["latitude"].ToString());
                            item.lng = float.Parse(row["longitude"].ToString());
                            item.marker_class = new List<RouteMarker_Marker>();

                            DataRow[] item2 = DT.Select(string.Format("stopid='{0}'",item.id));

                            foreach (DataRow dr in item2) {
                                RouteMarker_Marker marker = new RouteMarker_Marker();
                                if (dr["MarkerClassID"].ToString() == "" && dr["classname"].ToString() == "") {  break; };
                                marker.id = dr["MarkerClassID"].ToString().Trim();
                                marker.name = dr["classname"].ToString().Trim();
                                item.marker_class.Add(marker);
                            }
                            result.stop.Add(item);
                            stopid = item.id.ToString(); 
                        }
                    }
                    DT.Clear();
                    Response.Write(MyDb.JSON.stringify(result));
                    result = null;
                }
                Response.End();
            }
        }
    }
}
